var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var MPage = require('../MPage.js');
var app = getApp();

Page(common.extendPage(MPage, {
    data: {
        winHei: 0,
        dataLoaded: false,
        userInfo: {},
        loginPanel: {
            yzmTime: 0,
            phone: '',
            yzm: '',
            isFenxiao: false
        },
        isLoign: !!app.getUserSid(),
        canPay: false,
        isMeiYe: 0, //是否是美业专属版
    },

    onLoad: function() {
        this.setData({
            winHei: app.getWinHei(),
            isMeiYe: app.globalData.appType.indexOf(app.config.appTypeMap.meiye) > -1
        });
    },

    onShow() {
        var that = this;

        app.getUserInfo(function(userInfo) {
            that.setData({
                isLoign: !!app.getUserSid(),
                userInfo: userInfo,
                dataLoaded: true,
                isFenxiao: app.getIsFenxiao() == 1,
                techId: app.globalData.techId,
                canPay: app.getCanPay()
            });
            if (app.getIsFenxiao() != 1) {
                that.validateFx();
            }
        })
        this.getMyInfo();
    },

    onLoginSuc() {
        var that = this;

        app.getUserInfo(function(userInfo) {
            that.setData({
                isLoign: true,
                userInfo: userInfo,
                dataLoaded: true,
                isFenxiao: app.getIsFenxiao() == 1,
                techId: app.globalData.techId,
                canPay: app.getCanPay()
            });
            if (app.getIsFenxiao() != 1) {
                that.validateFx();
            }
        })
        this.getMyInfo();
    },

    _showLogin() {
        this.setData({
            isLoginShow: true
        });
    },

    _jump(evt) {
        var ty = evt.currentTarget.dataset.ty;
        var formId = evt.detail.formId;

        if (!this.data.isLoign) {
            this._showLogin();
            return;
        }

        if (ty == 'jishi') {
            let techId = this.data.techId;

            wx.navigateTo({
                url: techId && techId != 0 ? `/pages/appointOrderList/appointOrderList?techId=${techId}` : '/pages/waiterList/waiterList'
            });
            return;
        }

        var dataset = evt.currentTarget.dataset;
        var openType = dataset['openType'];
        var url = dataset.url;

        if (openType == 'navigate' || !openType) {
            openType = 'navigateTo';
        }

        wx[openType]({
            url
        });
        common.submitForm(formId);
    },

    /**
     * 手机号输入
     */
    _phoneInput: function(evt) {
        var panel = this.data.loginPanel;

        panel.phone = evt.detail.value;
        this.setData({
            panel
        });
    },

    /**
     * 验证码输入
     */
    _yzmInput: function(evt) {
        var panel = this.data.loginPanel;

        panel.yzm = evt.detail.value;
        this.setData({
            panel
        });
    },

    /**
     * 获取验证码
     */
    _getYzm() {
        if (!this.data.panel.phone) {
            return util.alert('请输入手机号');
        }
        ajax.P('/openapi/CBrand/sendMsg2', {
            phone: this.data.panel.phone,
        }, function(data) {
            this._timerDown(60);
        }.bind(this));
    },

    /**
     * 倒计时
     */
    _timerDown: function(time) {
        var panel = this.data.loginPanel;

        panel.yzmTime = --time;
        this.setData({
            loginPanel: panel
        });

        if (panel.yzmTime == 0) {
            return;
        }
        setTimeout(function() {
            this._timerDown(time);
        }.bind(this), 1000);
    },

    /**
     * 登陆
     */
    _login() {
        var self = this;
        var loginPanel = this.data.loginPanel;

        ajax.P('/openapi/CBrand/bandPhone2', {
            code: loginPanel.yzm,
            phone: loginPanel.phone,
            openId: app.getOpenId(),
        }, function(data) {
            var userSid = data.result.userSid;

            app.setUserSid(userSid);
            self.setData({
                isLogin: !!userSid
            });
        });
    },
    getMyInfo: function() {
        var _self = this;
        ajax.P('/openapi/CCoupon/getCouponAmount', {}, function(data) {
            _self.setData({
                couponAmount: data.result.couponAmount || 0,
                dataLoaded: true
            })
        });
    },

    validateFx: function() {
        var _self = this;

        if (this.data.isLoign && app.getIsFenxiao() != 1) {
            ajax.G('/openapi/CHome/isFenxiao', {}, function(data) {
                var isFenxiao = data.msg.is_fenxiao;

                app.setIsFenxiao(isFenxiao);

                _self.setData({
                    isFenxiao: data.msg.is_fenxiao == 1
                })
            })
        }
    }

}));